#include "MapMap.h"
#include "ui_mapmap.h"

#include <QFileDialog>

#include "GlobalManage.h"
#include "interactivemap.h"

MapMap::MapMap(GlobalManage * global,QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MapMap)
    ,m_global(global)
{
    ui->setupUi(this);
    m_InteractiveMap = ui->centralwidget;
    IniMap();
}

MapMap::~MapMap()
{
    ConfigureFileRecords();
    delete ui;
    delete m_global;
}


void MapMap::on_actionopenMap_triggered()
{
    QString path = QFileDialog::getExistingDirectory(this, tr("选择海图路径"));
    if(!path.isEmpty())
    {
        m_global->m_MapFilePath = path;
        m_InteractiveMap->setTilePath(m_global->m_MapFilePath);
    }
}

void MapMap::IniMap()
{
    m_InteractiveMap->setTilePath(m_global->m_MapFilePath);
    m_InteractiveMap->setDragMode(QGraphicsView::ScrollHandDrag);
    m_InteractiveMap->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_InteractiveMap->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    m_InteractiveMap->setDragMode(QGraphicsView::ScrollHandDrag);
    m_InteractiveMap->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);

    m_InteractiveMap->setTMSMode(false);

    m_InteractiveMap->centerOn(m_global->m_CenterOn);
    m_InteractiveMap->setZoomLevel(m_global->m_ZoomLevel);
}

void MapMap::ConfigureFileRecords()
{
    m_global->m_ZoomLevel = m_InteractiveMap->zoomLevel();//记录缩放比例
    m_global->m_CenterOn =  m_InteractiveMap->centerCoordinates();
    m_global->WriteConfigurationFile();//保存在配置文件
}

